package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/9/7.
 */
public class PersonInHosTransfer extends AbstractTransfer {

    public PersonInHosTransfer(String method, Client client, String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idNo = (String) params.get("idNo");
        Document document = getTplDoc("GetPatInHos");
        Element dataRow = document.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRow.addAttribute("ID_NO", idNo).addAttribute("PATIENT_ID", "");
        return document.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new LinkedHashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            PersonInHosInnerMessage personInHosInnerMessage = new PersonInHosInnerMessage(dataRow);
            resMap.put("inPatientId", personInHosInnerMessage.getInPatientId());
            resMap.put("patientId", personInHosInnerMessage.getPatientId());
            resMap.put("visitId", personInHosInnerMessage.getVisitId());
            resMap.put("name",personInHosInnerMessage.getName());
            resMap.put("sex", personInHosInnerMessage.getSex());
            resMap.put("dateOfBirth", personInHosInnerMessage.getDateOfBirth());
            resMap.put("idNo", personInHosInnerMessage.getIdNo());
            resMap.put("mailingAddress", personInHosInnerMessage.getMailingAddress());
            resMap.put("phoneNumberBussiness", personInHosInnerMessage.getPhoneNumberBussiness() );
            resMap.put("nextOfKin", personInHosInnerMessage.getNextOfKin());
            resMap.put("relationship", personInHosInnerMessage.getRelationship());
            resMap.put("nextOfKinPhone", personInHosInnerMessage.getNextOfKinPhone());
            resMap.put("admissionDateTime",personInHosInnerMessage.getAdmissionDateTime());
            resMap.put("dischargeDateTime", personInHosInnerMessage.getDischargeDateTime());
            resMap.put("days", personInHosInnerMessage.getDays());
            resMap.put("deptAdmissionYo",personInHosInnerMessage.getDeptAdmissionYo());
            resMap.put("wardName",personInHosInnerMessage.getWardName());
            resMap.put("bedNo", personInHosInnerMessage.getBedNo());
            resMap.put("attendingDoctor", personInHosInnerMessage.getAttendingDoctor());
            resMap.put("nurseResponsibility", personInHosInnerMessage.getNurseResponsibility());
            resMap.put("totalcharges", personInHosInnerMessage.getTotalcharges());
            resMap.put("totalprepay",personInHosInnerMessage.getTotalprepay());
            resMap.put("unsettled", personInHosInnerMessage.getUnsettled());
            resMap.put("settled", personInHosInnerMessage.getSettled());
            resMap.put("balance",personInHosInnerMessage.getBalance());
            resMap.put("status",personInHosInnerMessage.getStatus());
            resMap.put("phoneNumberHome",personInHosInnerMessage.getPhoneNumberHome());
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
      } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }

    private class PersonInHosInnerMessage implements InnerMessage {

        private String inPatientId;
        private String patientId;
        private String visitId;
        private String name;
        private String sex;
        private String dateOfBirth;
        private String idNo;
        private String mailingAddress;
        private String phoneNumberBussiness;
        private String nextOfKin;
        private String relationship;
        private String nextOfKinPhone;
        private String admissionDateTime;
        private String dischargeDateTime;
        private String days;
        private String deptAdmissionYo;
        private String wardName;
        private String bedNo;
        private String attendingDoctor;
        private String nurseResponsibility;
        private String totalcharges;
        private String totalprepay;
        private String unsettled;
        private String settled;
        private String balance;
        private String status;
        private String phoneNumberHome;


        public PersonInHosInnerMessage(Element element) {

            this.inPatientId = element.attributeValue("IN_PATIENT_ID");
            this.patientId = element.attributeValue("PATIENT_ID");
            this.visitId = element.attributeValue("VISIT_ID");
            this.name = element.attributeValue("NAME");
            this.sex = element.attributeValue("SEX");
            this.dateOfBirth = element.attributeValue("DATE_OF_BIRTH");
            this.idNo = element.attributeValue("ID_NO");
            this.mailingAddress = element.attributeValue("MAILING_ADDRESS");
            this.phoneNumberBussiness = element.attributeValue("PHONE_NUMBER_BUSINESS");
            this.nextOfKin = element.attributeValue("NEXT_OF_KIN");
            this.relationship = element.attributeValue("REALATIONSHIP");
            this.nextOfKinPhone = element.attributeValue("NEXT_OF_KIN_PHONE");
            this.admissionDateTime = element.attributeValue("ADMISSION_DATE_TIME");
            this.dischargeDateTime = element.attributeValue("DISCHARGE_DATE_TIME");
            this.days = element.attributeValue("DAYS");
            this.deptAdmissionYo = element.attributeValue("DEPT_ADMISSION_TO");
            this.wardName = element.attributeValue("WARD_NAME");
            this.bedNo = element.attributeValue("BED_NO");
            this.attendingDoctor = element.attributeValue("ATTENDING_DOCTOR");
            this.nurseResponsibility = element.attributeValue("NURSE_RESPONSIBILITY");
            this.totalcharges = element.attributeValue("TOTALCHARGES");
            this.totalprepay = element.attributeValue("TOTALPREPAY");
            this.unsettled = element.attributeValue("UNSETTLED");
            this.settled = element.attributeValue("SETTLED");
            this.balance = element.attributeValue("BALANCE");
            this.status = element.attributeValue("STATUS");
            this.phoneNumberHome = element.attributeValue("PHONE_NUMBER_HOME");

        }

        public String getInPatientId() {
            return inPatientId;
        }

        public String getPatientId() {
            return patientId;
        }

        public String getVisitId() {
            return visitId;
        }

        public String getName() {
            return name;
        }

        public String getSex() {
            return sex;
        }

        public String getDateOfBirth() {
            return dateOfBirth;
        }

        public String getIdNo() {
            return idNo;
        }

        public String getMailingAddress() {
            return mailingAddress;
        }

        public String getPhoneNumberBussiness() {
            return phoneNumberBussiness;
        }
        public String getPhoneNumberHome() {
            return phoneNumberHome;
        }

        public String getNextOfKin() {
            return nextOfKin;
        }

        public String getRelationship() {
            return relationship;
        }

        public String getNextOfKinPhone() {
            return nextOfKinPhone;
        }

        public String getAdmissionDateTime() {
            return admissionDateTime;
        }

        public String getDischargeDateTime() {
            return dischargeDateTime;
        }

        public String getDays() {
            return days;
        }

        public String getDeptAdmissionYo() {
            return deptAdmissionYo;
        }

        public String getWardName() {
            return wardName;
        }

        public String getBedNo() {
            return bedNo;
        }

        public String getAttendingDoctor() {
            return attendingDoctor;
        }

        public String getNurseResponsibility() {
            return nurseResponsibility;
        }

        public String getTotalcharges() {
            return totalcharges;
        }

        public String getTotalprepay() {
            return totalprepay;
        }

        public String getUnsettled() {
            return unsettled;
        }

        public String getSettled() {
            return settled;
        }

        public String getBalance() {
            return balance;
        }

        public String getStatus() {
            return status;
        }
    }


}
